package com.epfresh.gradient.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

import com.epfresh.gradient.inter.Gradually;
import com.epfresh.gradient.inter.SyncGradually;

/**
 * 渐变ActionBar绑定的ScrollView
 *
 * @author 陈小默
 */
public class GradientScrollView extends ScrollView implements SyncGradually {
    private int height_actionBar;
    private int width_actionBar;
    private Gradually actionBar;
    private onScrollChangedListener listener;

    /**
     * 获得与之绑定的ActionBar的高度
     *
     * @return
     */
    public int getHeight_actionBar() {
        return height_actionBar;
    }

    /**
     * 获得与之绑定的ActionBar的宽度
     *
     * @return
     */
    public int getWidth_actionBar() {
        return width_actionBar;
    }

    public GradientScrollView(Context context, AttributeSet attrs,
                              int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 绑定方法
     *
     * @param actionBar
     */
    public void bind(Gradually actionBar) {
        this.actionBar = actionBar;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        setGradeWithHigh(t);
        if (listener != null) {
            listener.onScrollChanged(l, t, oldl, oldt, height_actionBar);
        }
    }

    public void setGradeWithHigh(int t) {
        if (actionBar != null) {
            //change the grade color flash bug（modified by wzh）
            t = t > 0 ? t : 0;
            actionBar.setGrade(t >= height_actionBar ? 0
                    : ((height_actionBar - t) * 0xff / height_actionBar));
        }
    }

    /**
     * 设置滑动事件监听器
     *
     * @param listener
     */
    public void setOnScrollChangedListener(onScrollChangedListener listener) {
        this.listener = listener;
    }

    public GradientScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GradientScrollView(Context context) {
        this(context, null);
    }

    @Override
    public void syncDimention(int height, int width) {
        this.height_actionBar = height;
        this.width_actionBar = width;
    }

    /**
     * 滑动事件监听器
     *
     * @author 陈小默
     */
    public interface onScrollChangedListener {
        /**
         * @param l
         * @param t
         * @param oldl
         * @param oldt
         */
        void onScrollChanged(int l, int t, int oldl, int oldt, int totalHeight);
    }

}
